package com.ywm.staService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ywm.commonUtil.R;
import com.ywm.staService.client.UcenterClient;
import com.ywm.staService.entity.StatisticsDaily;
import com.ywm.staService.mapper.StatisticsDailyMapper;
import com.ywm.staService.service.StatisticsDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-06-14
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;
    @Override
    public void registerCount(String day) throws Exception {
        //添加之前删除表相同日期的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);
        //远程调用得到某一天的人数
        R r = ucenterClient.countRegister(day);
        int count =(int)r.getData().get("countRegister");
        Random random=new Random();
        Integer loginNum = random.nextInt(100)+100;//TODO 登录人数
        Integer videoViewNum = random.nextInt(100)+100;//TODO 视频播放数
        Integer courseNum = random.nextInt(100)+100;//TODO 课程数
        //将获取到的数据添加到数据库
        //创建统计对象
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(count);
        daily.setLoginNum(loginNum);
        daily.setVideoViewNum(videoViewNum);
        daily.setCourseNum(courseNum);
        daily.setDateCalculated(day);

        int insert = baseMapper.insert(daily);
        if(insert<=0){
            throw new Exception("插入昨日数据");
        }
    }

    //图标显示，返回两部分数据，日期json数组，数量json数组
    @Override
    public Map<String, Object> showDate(String type, String begin, String end) {
        //根据条件查询对应的数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        //指定查询的列
        wrapper.select("date_calculated",type);
        wrapper.orderByAsc("date_calculated");
        List<StatisticsDaily> list = baseMapper.selectList(wrapper);
        ArrayList<String> data_calculatedList = new ArrayList<>();
        ArrayList<Integer> numDataList = new ArrayList<>();
        //遍历查询出来的数据，进行封装
        for (StatisticsDaily daily : list) {
            data_calculatedList.add(daily.getDateCalculated());
            switch (type){
                case "login_num":
                    numDataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    numDataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    numDataList.add(daily.getCourseNum());
                    break;
                case "register_num":
                    numDataList.add(daily.getRegisterNum());
                    break;
                default:
                    break;
            }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("data_calculatedList",data_calculatedList);
        map.put("numDataList",numDataList);
        return map;
    }
}
